package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.CarouselReqDTO;
import com.iafme.dto.res.CarouselResDTO;
import com.iafme.model.Carousel;
import com.iafme.service.CarouselService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/25 09:55
 */
@RestController
@RequestMapping(value = "/cooperate")
@AllArgsConstructor
public class CooperateController implements BaseCommonInterface<CarouselReqDTO, CarouselResDTO> {

    private final CarouselService carouselService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "网站设置-合作伙伴")
    @SaCheckPermission("website:cooperate:add")
    @Override
    public Result<String> save(@Valid CarouselReqDTO carouselReqDTO) {
        Carousel carousel = Convert.convert(Carousel.class, carouselReqDTO);
        carouselService.save(carousel);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "网站设置-合作伙伴")
    @SaCheckPermission("website:cooperate:edit")
    @Override
    public Result<String> update(@Valid CarouselReqDTO carouselReqDTO) {
        Carousel carousel = Convert.convert(Carousel.class, carouselReqDTO);
        carouselService.updateById(carousel);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "网站设置-合作伙伴")
    @SaCheckPermission("website:cooperate:delete")
    @Override
    public Result<String> del(Integer id) {
        carouselService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<CarouselResDTO>> lists(CarouselReqDTO carouselReqDTO) {
        return Result.successResult(carouselService.lists(carouselReqDTO));
    }

    @Override
    public Result<CarouselResDTO> findById(Integer integer) {
        return null;
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "网站设置-合作伙伴")
    @SaCheckPermission("website:cooperate:delete")
    @Override
    public Result<String> batchDel(List<CarouselResDTO> list) {
        List<Integer> ids = list.stream().map(CarouselResDTO::getId).collect(Collectors.toList());
        carouselService.removeByIds(ids);
        return Result.successResult();
    }
}

